<?php

namespace app\api\model\circle;


use app\api\model\UserModel;
use app\logic\model\circle\CircleCommentLogicModel;
use app\logic\model\user\UserLogicModel;

class CircleCommentApiModel extends CircleCommentLogicModel
{
    public function getApiList(array $param,$where=[],$filed="*",$page=1,$limit=15,$order="id desc"){
        $query = $this->where($where)
            ->field($filed)
            ->where(function($q) use ($param){
                if(check_request($param,'c_id')){
                    $q->where('c_id',$param['c_id']);
                }
                if(check_request($param,'status')){
                    $q->where('status',$param['status']);
                }
                if(check_request($param,'audit')){
                    $q->where('audit',$param['audit']);
                }
            });
        $count = $query->count();
        $list = $query
            ->withCount([
                'likes'=>'like_count',
            ])->page($page,$limit)
            ->order($order)
            ->select()->toArray();
        if(!empty($list)){
            $user_id_arr = array_column($list,'user_id');
            $users = UserModel::getInfosByIds($user_id_arr,'nickname,head_img');
            $apply_id_arr = array_column($list,'apply_id');
            $apply = $this->whereIn('id',$apply_id_arr)
                ->column('user_id,content,create_time','id');
            foreach ($list as &$val){
                $val['user'] = $users[$val['user_id']]??[];
                $now_apply = $apply[$val['apply_id']]??[];
                if(!empty($now_apply)){
                    $now_apply['user'] = UserLogicModel::getInfoById($now_apply['user_id'],'nickname,head_img');
                }
                $val['apply'] = $now_apply;
            }
            unset($val);
        }
        return [$list,$count];
    }
    function getFindById($id,$field='*'){
        return $this->field($field)->with(['user','circle','apply'])->find($id);
    }
}